package pe.edu.upc.dew.grupo5.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo5.service.PeliculaService;
import pe.edu.upc.dew.grupo5.model.Funcion;

/**
 * Servlet implementation class ComboHorarioPeliculaPorCineServlet
 */
public class ComboHorarioPeliculaPorCineServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PeliculaService peliculaService;

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.peliculaService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"peliculaService", PeliculaService.class);
	}
	
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String idCine = request.getParameter("idCine");
		String idPelicula = request.getParameter("idPelicula");
		List<Funcion> list = peliculaService.getHorarioDePelicula(Integer.valueOf(idCine),Integer.valueOf(idPelicula));
		try {
			
			for (Funcion funcion: list) {
				out.printf("<option value='%1s'>%2s</option>",funcion.getIdFuncion(),funcion.getHora());
			}
		} finally {
			out.close();
		}
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
